package com.dongdongshop.aop;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component //实例化
@Aspect   //切面类
@Slf4j
public class LogAop {

    //切点表达式
    @Pointcut("execution(public * com.dongdongshop.service.*.*(..)) || @annotation(com.dongdongshop.aop.DKLog)")
    public void log() {
        
    }
    
    //通知: 前置通知 后置通知  环绕通知  异常通知  返回后通知
    @Before("log()")
    public void before(JoinPoint joinPoint) {
        //取出所有的参数
        Object[] args = joinPoint.getArgs();
        //取出当前被切到的方法
        Signature signature = joinPoint.getSignature();
        log.info("当前被切到的方法为:{},参数为:{}",signature,args);
    }

    //返回后通知
    @AfterReturning(value = "log()",returning = "result")
    public void afterReturn(Object result){
        String json = JSONObject.toJSONString(result);
        log.info("方法执行成功,返回值:{}",json);
    }
}
